clear all; 
clc; 
close all; 
%-------------------------------------------------------------------------- 
% Relevant parameter defination with system 
lamda  = 1;                     % Wavelength of the incoming signal 
K      = 1*8;                   % Total number of array elements  
d      = lamda/2;               % Inter-element spacing    
f1     = 0.1;                   % Frequency of the incoming signal 
f2     = 0.2;                   % Frequency of the interferer signal 
deg0   = [ 30 ];                % Angle of arrival of desired signal in radians 
degi   = [ -50 -30 10 50];      % Angle of arrival of interferer in radians 
theta0 = deg0.*pi/180;          % theta0 
thetai = degi.*pi/180;          % thetai 
SNR    = 10;                    % Signal to Noise Ratio in dB 
SIR    = [3  5  7  9];          % Signal to Interferer Ratio in dB 
Ni     = 4;                     % Number of interferers 
dw     = 0.5 ;                   % d to lamda ratio of Inputing 
no_samples = 3000;              % Number of samples 
kap         = (2*pi)/lamda; 
%-------------------------------------------------------------------------- 
% Modualation for In_Sequence 
ipBit=randn(1,no_samples)>0.5 
ip=2*ipBit-1 
 
%-------------------------------------------------------------------------- 
% Generating DirectionMatrix of desired_siganl 
alpha1 = kap*d*sin(theta0); 
index1 = 0:K-1; 
vtranspose = exp(-1i*(index1)*alpha1);  % DirectionVector of desired signal 
 
%-------------------------------------------------------------------------- 
% Generation of signal with Signal Power = 1 
index2 = 1:no_samples; 
signal = sqrt(2)*cos(2*pi*f1*index2); % A = 1 
 
%-------------------------------------------------------------------------- 
% Addition of Gaussian noise 
sig_noisepower = 1/(10^(SNR/10)); 
sig_noise      = randn(1,no_samples); 
sig_noise      = sqrt(sig_noisepower)*sig_noise; 
 
%-------------------------------------------------------------------------- 
% Generating Signal with Modulation 
s = ip.*signal; 
 
%-------------------------------------------------------------------------- 
ntranspose =zeros(Ni,length(index1)); 
interferer_signal          =zeros(Ni,no_samples); 
% Generating Inteferer with Signal Power = A^2/2 
for index3     = 1:Ni 
    alpha2     = kap*d*sin(thetai(index3)); 
    ntranspose(index3,:) = exp(-1i*(index1)*alpha2);%DirectionVector of inteferer signal 
 
    %---------------------------------------------------------------------- 
    % Generating Inteferer with Signal Power = A^2/2 
    interferer_signal      = cos(2*pi*f2*index2); 
    int_noisepower         = 1/(10^(SIR(index3)/10)); 
    int_noise              = sqrt(2*int_noisepower); 
     
    %---------------------------------------------------------------------- 
    % Generating Interferer Signal multiplied with respective amplitude 
    interferer_signal(index3,:) = int_noise*interferer_signal; % Signal = Acos(wt)  
end 
 
%-------------------------------------------------------------------------- 
% Adding Noise For Normalization 
Noise=randn(K,no_samples); 
npow=1/(10^(SNR/10)); 
Noise=sqrt(npow)*Noise; 
%-------------------------------------------------------------------------- 
% generation DirectionMatrix (C0)with signal and interferer_siganl 
DirectionMatrix=[vtranspose' ntranspose(1,:)' ntranspose(2,:)' ntranspose(3,:)' ntranspose(4,:)']; 
 
%-------------------------------------------------------------------------- 
% Generating the signal and interferer 
signal_interferer=[s' interferer_signal']'; 
%signal_interferer0=[s' u(1,:)' u(2,:)' u(3,:)' u(4,:)']' %the same meaning right 
%-------------------------------------------------------------------------- 
% genrating the output signal called OutPutMatrix 
OutPutMatrix=DirectionMatrix*signal_interferer+Noise+j*Noise; 
 
%-------------------------------------------------------------------------- 
% LCMV Algorithm finding w_opt 
%RelativeMatrix=OutPutMatrix*OutPutMatrix'/no_samples; 
%F=[1 0 0 0 0]' 
%WeightVector=inv(RelativeMatrix)*DirectionMatrix*inv(DirectionMatrix'*inv(RelativeMatrix)*DirectionMatrix)*F;%LCMV Algorithm w_opt 
%-------------------------------------------------------------------------- 
% Bartlett Algorithm 
Bartlett_temp4=sqrt(DirectionMatrix(:,1)'*DirectionMatrix(:,1)); 
WeightVector=DirectionMatrix(:,1)/Bartlett_temp4; 
%-------------------------------------------------------------------------- 
% Signal with signal_WeightVector 
signal_WeightVector=OutPutMatrix'*WeightVector; 
signal_WeightVector0=reshape(signal_WeightVector,1,[]); 
%-------------------------------------------------------------------------- 
% Demodualation  
ip_Hat0=(real(signal_WeightVector0/2)); 
ip_Hat(find(ip_Hat0>0))=+1; 
ip_Hat(find(ip_Hat0<0))=-1; 
% Detection 
ipBitHat=(ip_Hat+1)/2; 
%-------------------------------------------------------------------------- 
% Plot the figures 
%-------------------------------------------------------------------------- 
% Display the figure of Array & Polar of Smart Anntennas 
L=0;   % For search  
for angle1=-90:90  
   L=L+1;  
   index1=0:K-1;  
   v_temp=j*2*index1*pi*dw; 
   v=exp(v_temp*sin(angle1*pi/180)); 
   Direction(:,L)=v;   
end 
 
%-------------------------------------------------------------------------- 
% Direction vector including the expection & interfere signal 
Arrfact(1,:)=WeightVector'*Direction;  
Arrfact=abs(Arrfact);  
Arrfactmax=max(Arrfact);  
Pattern=Arrfact/Arrfactmax;  %For Normalization 
%-------------------------------------------------------------------------- 
% Plot the figure of Rectangular Plot of Array Factor  
angle1=-90:90;  
PatterndB=20*log10(Pattern);  
plot(angle1,PatterndB,'r-','linewidth',1.50); 
title('Rectangular Plot of Array Factor');  
xlabel('theta(deg)');  
ylabel(' |Array Factor|/dB');  
%legend('LCMV-Algorithm') 
axis([-90 90 -30 0]); 
set(gca,'xtick',-90:10:90,'fontsize',8) 
grid on  
hold on; 
%-------------------------------------------------------------------------- 
% Plot the figure of Polar Plot of Array Factor 
figure 
polar(-pi/2:pi/180:pi/2,abs(Pattern)) 
title('Polar Plot of Array Factor') 
%-------------------------------------------------------------------------- 
% scatterplot 
% scatterplot(ipBitHat) 
%-------------------------------------------------------------------------- 
% save file 
save Bartlett_Algorithm_Ex_2_21_yzliu 